package com.smallA.controller;

import com.smallA.constant.DataConstant;
import com.smallA.po.Exam;
import com.smallA.po.TopicType;
import com.smallA.service.ExamService;
import com.smallA.service.QuestionService;
import com.smallA.service.TestService;
import com.smallA.vo.Result;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TestController {

    @Resource
    private TestService testService;
    @Resource
    private ExamService examService;
    @Resource
    private QuestionService questionService;

    /**
     * 获取所有的题目类型
     */
    @RequestMapping("/Test/getTopicType")
    public Object getTopicType() {
        List<TopicType> topicTypes = testService.selectAllTopicTpe();
        return topicTypes;
    }

    /**
     * 自动出卷调用接口
     */
    @RequestMapping("/Test/autoTest")
    public Object autoTest(Integer teacherId, Integer[] topicTypeIds, Integer choiceCount, Integer fillCount, Integer shortAnswerCount, String context, int score, HttpSession session) {
        return examService.autoExam(topicTypeIds, choiceCount, fillCount, shortAnswerCount, context, teacherId, score);
    }

    /**
     * 这个就是单纯获取下试卷的数据然后展示出来而已
     */
    @RequestMapping("/Test/getExam")
    public Object getExam() {
        return examService.selectAllExam();
    }

    /**
     * 通过id获取试卷的详细信息
     */
    @RequestMapping("/Test/getExamById")
    public Object getExamById(Integer eid) {
        if (eid == null) {
            return null;
        }
        return examService.selectExamByExamId(eid);
    }

    /**
     * 添加一场考试用接口
     */
    @RequestMapping("/Test/addTest")
    public Object addTest(@DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime, Integer examId, Integer useTime, String context, Integer detailBegin) {
        //开始操作
        Date date = new Date(beginTime.getTime() + detailBegin * 1000 * 60 * 60);
        return testService.insertTest(date, examId, useTime, context);
    }

    @RequestMapping("/Test/markExam")
    public Object markExam(int gid){
        Integer tid = testService.selectTestByGradeId(gid);
        Result<Exam> examResult = examService.selectExamById(gid);
        Map<String, Object> map = new HashMap<>();
        map.put(DataConstant.EXAM_DETAIL, examResult.getData());
        map.put(DataConstant.ID_GRADE, gid);
        return map;

    }

    @RequestMapping("/Test/calculate")
    public boolean calculate(Float[] fills,Float[] shorts,int gid) {
        Result<Exam> examResult = examService.selectExamById(gid);
        return testService.markGrade(fills, shorts, examResult.getData(), gid);

    }

    /**
     * 上传excel表格用方法
     */
    @RequestMapping("/Test/upload")
    public Object excelUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        /*上传路径保存设置*/
        String path = "C:\\Users\\啦啦啦\\Desktop\\File";
        // request.getServletContext().getRealPath("/static/upload");
        return questionService.insertQuestion(path, file);
    }

    @RequestMapping("/Test/downloadTemplate")
    public void downloads(HttpServletResponse resp, HttpServletRequest req) throws Exception {
        questionService.dowmloadTemplate(resp, req);
    }

    /**
     * 这个就是单纯的查看所有的考试
     */
    @RequestMapping("/Student/getTest")
    public Object getTests() {
        return testService.selectAllTest();
    }
}
